﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using NDF.DataAccess.EntityFramework;
using NDF.Test.DataAccess.EFTest;
using System;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

namespace NDF.Test.DataAccess
{
    [TestClass]
    public class EntityFrameworkTest
    {
        [TestMethod]
        public void TestMethod1()
        {
            //Database.SetInitializer<BloggingContext>(new DropCreateDatabaseAlways<BloggingContext>());

            using (var db = new BloggingContext())
            {
                //db.Database.CreateIfNotExists();

                // Create and save a new Blog 
                //Console.Write("Enter a name for a new Blog: ");
                //var name = Console.ReadLine();

                var blog = new Blog { Name = "Java && Eclipse" };
                db.Blogs.Add(blog);
                db.SaveChanges();

                // Display all Blogs from the database 
                var query = from b in db.Blogs
                            orderby b.Name
                            select b;

                //Console.WriteLine("All blogs in the database:");
                foreach (var b in query)
                {
                    Console.WriteLine(b.Name);

                    foreach (var p in b.Posts)
                    {
                        Console.WriteLine(p.Title);
                    }

                    Console.WriteLine("---------");
                }

                //Console.WriteLine("Press any key to exit...");
                //Console.ReadKey(); 
            }
        }


        [TestMethod]
        public void TestMethod2()
        {
            using (var db = new BloggingContext())
            {
                DataTable table = db.NDFDatabase.ExecuteDataTable("SELECT * FROM sys.tables WHERE name LIKE @name + N'%'", "B");
                foreach (DataRow row in table.Rows)
                {
                    Console.WriteLine(row["name"]);
                }


                //DataTable table = db.NDFDatabase.ExecuteDataTable("SELECT * FROM sys.tables");
                //foreach (DataRow row in table.Rows)
                //{
                //    Console.WriteLine(row["name"]);
                //}

                //var blogs = from b in db.Blogs group b by b.Name into bb select bb.FirstOrDefault();
                //foreach (var b in blogs)
                //{
                //    Console.WriteLine(b.Name);
                //}
            }
        }
    }
}
